﻿

//物品记录

var mEvidenceBillList_Query = {};//查询条件

var mEvidenceBillList_ModeType;//显示类型 0=普通模式， 1= 选择模式

$(function () {

    var args = getUrlArgs();
    mEvidenceBillList_ModeType = parseInt(args["modeType"]);
    var tbt = parseInt(args["targetBillType"]);
    mEvidenceBillList_Query.TargetBillType = isNaN(tbt) ? 0 : tbt;// parseInt(args["targetBillType"]);//查询的源单类型

    $("#cmbState").combobox({
        valueField: 'Id',
        textField: 'Name',
        editable: false,
        onSelect: function (record) {
            mEvidenceBillList_Query.StockState = record.Id;
        }
    });

    $("#cmbOrg").combobox({
        valueField: 'Id',
        textField: 'Name',
        editable: false,
        onSelect: function (record) { mEvidenceBillList_Query.DeptId = record.Id; }
    });

    gLoadAllDepts(function (orgs) {
        var allOrg = {};
        allOrg.Id = 0;
        allOrg.Name = "全部";
        orgs.splice(0, 0, allOrg);
        $('#cmbOrg').combobox('loadData', orgs);
    }, false);

    setCmbStateData();


    packQuery();//打包初始查询条件

    $('#evidenceBillDataGrid').datagrid({
        url: '/Case/LoadEvidenceBillSimpleList',
        fitColumns: false,
        rownumbers: true,
        resizable: true,
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        nowrap: false,
        height: 540,
        idField: 'Id',
        toolbar: '#evidenceBillToolbar',
        queryParams: { 'evidenceBillQuery': JSON.stringify(mEvidenceBillList_Query) },
        singleSelect: true,
        onDblClickRow: selEvidenceRow,
        columns: [[
               { title: 'Id', field: 'Id', align: 'left', width: 10, hidden: true },
               { title: '状态', field: 'IsCheck', align: 'left', width: 100, formatter: getStateName },
               { title: '案件编号', field: 'Number', align: 'left', width: 100 },
               { title: '单据编号', field: 'SerialNo', align: 'left', width: 100 },
               { title: '记录日期', field: 'BillTime', align: 'center', width: 100, formatter: getFormatDate },
               { title: '办案单位', field: 'TransferDeptName', align: 'left', width: 150, formatter: gFormatDeptNameForColumn },
               { title: '办案人', field: 'TransferName', align: 'left', width: 80 },
               { title: '保管部门', field: 'ManageDeptName', align: 'left', width: 200, formatter: gFormatDeptNameForColumn }
        ]]
    });
    setting();
    //loadEvidenceBills();
});


function setCmbStateData() {
    var states = [];

    var s1 = {};
    s1.Id = -1;
    s1.Name = "全部未结案";

    var s2 = {};
    s2.Id = -2;
    s2.Name = "未审核"

    var s3 = {};
    s3.Id = -4;
    s3.Name = "已审核,未入库";

    var s4 = {};
    s4.Id = 1;
    s4.Name = "部分入库";

    var s5 = {};
    s5.Id = 2;
    s5.Name = "全部入库";

    var s6 = {};
    s6.Id = 32;
    s6.Name = "全部出库,待结案";

    var s7 = {};
    s7.Id = -8;
    s7.Name = "已结案";

    var s8 = {};
    s8.Id = -16;
    s8.Name = "全部";

    states.push(s1, s2, s3, s4, s5, s6, s7, s8);

    $("#cmbState").combobox('loadData', states);

    $("#cmbState").combobox('select', -1);
}

//
function selEvidenceRow(rowIndex, rowData) {
    if (window.parent.onSelectSourceBill) {
        window.parent.onSelectSourceBill(1, rowData.Id); //sourceType =物品单 =1; 
        window.parent.mStockBill_Query_Dlg.close();
    }else{
        btnEditEvidenceBillAction();
    }
}

//
function setting() {
    if (mEvidenceBillList_ModeType == 1) {
        $('#btnAddEvidenceBill').hide();
        $('#btnEditEvidenceBill').hide();
        $('#btnDelEvidenceBill').hide();
        $('#btnCloseEvidenceBill').hide();
    }
}

function getStateName(value, rowData, index) {
    if (!value && !rowData.IsFinish) {
        return '未审核';
    }
    else if (value && !rowData.IsFinish) {
        if (rowData.StockState == 1)
            return "部分入库";
        else if (rowData.StockState == 2)
            return "全部入库";
        else if (rowData.StockState == 4)
            return "部分调查入库";
        else if (rowData.StockState == 8)
            return "全部调查入库";
        else if (rowData.StockState == 16)
            return "部分出库";
        else if (rowData.StockState == 32)
            return "全部出库";
        else if (rowData.StockState == 64)
            return "部分调查出库";
        else if (rowData.StockState == 128)
            return "全部调查出库";
        else if (rowData.StockState == 256)
            return "结案";
        else
            return "未入库"

    } else if (rowData.IsFinish) {
        return '结案';
    } else {
        return '状态未定义,value=' + value + "  ,IsFinish= " + rowData.IsFinish;
    }

}

function getFormatDate(value, rowData, index) {
    if (value) {
        return gFormatDateFormCSharp(value, 'yyyy-MM-dd');
    }
}
//打包参数
function packQuery() {
    mEvidenceBillList_Query.Number = $('#txtCaseNo').val();
    mEvidenceBillList_Query.BeginTime = $('#dteBeginTime').datebox('getValue');
    mEvidenceBillList_Query.EndTime = $('#dteEndTime').datebox('getValue');
    mEvidenceBillList_Query.TransferName = $('#txtTransferName').val();
    mEvidenceBillList_Query.EvidenceName = $('#txtEvidenceName').val();
    mEvidenceBillList_Query.SerialNo = $('#txtSerialNo').val();
}

//读取所有物品单据
function loadEvidenceBills() {

    try {
        packQuery();
        var json = JSON.stringify(mEvidenceBillList_Query);
        $('#evidenceBillDataGrid').datagrid('reload', { 'evidenceBillQuery': json });

    } catch (ex) {
        alert(ex);
    }
}

function getEvidenceBillSelected() {
    var row = $("#evidenceBillDataGrid").datagrid("getSelected");
    var id = 0;
    if (row) {
        id = row.Id;
    }
    return id;
}

function btnAddEvidenceBillAction() {

    try {
        mStorageViewDlg = art.dialog({
            id: "diaFormStorageView",
            title: '物品记录',
            content: "<iframe scrolling='yes' frameborder='0' src='/Case/EvidenceBillView?evidenceBillId=0' style='width:810px;height:450px;'/>",
            lock: true,
            initFn: function () {
            }
        });
    } catch (ex) {
        alert(ex);
    }
}

function btnEditEvidenceBillAction() {
    try {

        var billId = getEvidenceBillSelected();

        mStorageViewDlg = art.dialog({
            id: "diaFormStorageView",
            title: '物品记录',
            content: "<iframe scrolling='yes' frameborder='0' src='/Case/EvidenceBillView?evidenceBillId=" + billId + "' style='width:810px;height:450px;'/>",
            lock: true,
            initFn: function () {
            }
        });
    } catch (ex) {
        alert(ex);
    }
}

function btnSearchEvidenceBillListAction() {
    loadEvidenceBills();
}

function btnDelEvidenceBillAction() {

    checkSession();

    try {

        var row = $("#evidenceBillDataGrid").datagrid("getSelected");
        var billId = 0;
        var allow = false;

        if (row) {
            billId = row.Id;

            if (row.IsCheck) {

                if (row.StockState > 0 ) {
                    $.messager.alert("操作提示", "该物品单据已经入库，不能删除", "warning");
                    return;
                } else {
                    if (gUserIsPermission("stockMgr")) {
                        allow = true;
                    }
                }

                $.messager.alert("操作提示", "该物品单据已经通过审核，不能删除", "warning");
                return;
            }else
            {
                allow = true;
            }
        } else {
            $.messager.alert("提示", "请选择要审核的数据", "warning");
            return;
        }
        if (allow) {
            $.messager.confirm('操作提示', "确定删除编号为： " + row.Number + " 的案件物品记录?", function (r) {
                if (r) {
                    $.ajax({
                        url: "/Case/DeleteEvidenceBill",
                        type: "POST",
                        dataType: "json",
                        data: { "Id": billId },
                        async: false,
                        success: function (req) {
                            if (req.IsSuccess) {
                                $.messager.alert('提示', '删除成功!');
                                $('#evidenceBillDataGrid').datagrid("reload");
                            } else {
                                $.messager.alert('提示', req.Data, "warning");
                            }
                        }
                    });
                }
            });
        }
       
    } catch (ex) {
        alert(ex);
    }
}
//结案
function closeEvidenceBillAction() {
    var row = $("#evidenceBillDataGrid").datagrid("getSelected");
    var id = 0;
    if (row) {
        if (row.IsFinish) {
            $.messager.alert('提示', '物品单已经结案!');
        }
        else if (row.StockState != 32) {
            $.messager.alert('提示', '物品单还未完全出库,不能结案!');
        } else if (row.StockState == 32) {
            $.ajax({
                url: "/Case/CloseEvidenceBill",
                type: "POST",
                dataType: "json",
                data: { "billId": row.Id },
                async: false,
                success: function (req) {
                    if (req.IsSuccess) {
                        $.messager.alert('提示', '结案成功!');
                        loadCloseEvidences();
                    } else {
                        $.messager.alert('提示', '结案失败! ' + req.Data);
                    }
                }
            });
        }
    } else {
        $.messager.alert('提示', '请选择物品单!');
    }
}

//导出物品列表数据到excel
function btnExportEvidenceToExcelAction() {
    packQuery();
    var json = JSON.stringify(mEvidenceBillList_Query);
    var jsonstr = encodeURIComponent(json);
    window.open("/PrintBill/PrintEvidenceBillList?queryParams=" + jsonstr); 
};